package com.sz.netty.service.impl;

import com.sz.netty.server.handler.BinaryDataHandler;
import com.sz.netty.service.DeviceCommandManager;
import com.sz.netty.service.DeviceCommandService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;

/**
 * Implementation of device command service
 */
@Slf4j
@Service
public class NettyDeviceCommandServiceImpl implements DeviceCommandService {
    
    @Override
    public boolean sendRemoteDisplayContent(String deviceId, String displayContent) {
        try {
            // Use the enhanced command manager for more reliable sending
            return DeviceCommandManager.sendRemoteDisplayContentReliable(deviceId, displayContent);
        } catch (Exception e) {
            log.error("Error sending remote display content to device {}: {}", deviceId, e.getMessage(), e);
            return false;
        }
    }
    
    @Override
    public boolean sendRemoteDisplayContent(String deviceId, byte[] displayContent) {
        try {
            // Convert bytes to string
            String contentStr = new String(displayContent, StandardCharsets.US_ASCII);
            // Use the enhanced command manager for more reliable sending
            return DeviceCommandManager.sendRemoteDisplayContentReliable(deviceId, contentStr);
        } catch (Exception e) {
            log.error("Error sending remote display content to device {}: {}", deviceId, e.getMessage(), e);
            return false;
        }
    }
    
    @Override
    public boolean sendRemoteDisplayContentWithAck(String deviceId, String displayContent) {
        try {
            // Validate input
            if (displayContent == null || displayContent.length() != 3) {
                log.warn("Invalid display content for device {}: expected 3 characters, got '{}'", deviceId, displayContent);
                return false;
            }
            
            // Convert string to bytes
            byte[] contentBytes = displayContent.getBytes(StandardCharsets.US_ASCII);
            
            // Send command and wait for acknowledgment
            return BinaryDataHandler.sendRemoteDisplayContent(deviceId, contentBytes, true);
        } catch (Exception e) {
            log.error("Error sending remote display content with acknowledgment to device {}: {}", deviceId, e.getMessage(), e);
            return false;
        }
    }
    
    @Override
    public boolean sendRemoteDisplayContentWithAck(String deviceId, byte[] displayContent) {
        try {
            // Send command and wait for acknowledgment
            return BinaryDataHandler.sendRemoteDisplayContent(deviceId, displayContent, true);
        } catch (Exception e) {
            log.error("Error sending remote display content with acknowledgment to device {}: {}", deviceId, e.getMessage(), e);
            return false;
        }
    }
    
    @Override
    public boolean isDeviceConnected(String deviceId) {
        return BinaryDataHandler.isDeviceConnected(deviceId);
    }
    
    @Override
    public String[] getConnectedDevices() {
        return BinaryDataHandler.getConnectedDevices();
    }
}